package eic.muckup.garden;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class WateringPlanerActivity extends ListActivity {
	
	private WateringCalendarAdapter wca;
	private WateringCalendarClass mWateringCalendar;
	private ArrayList<WateringCalendarClass> mWateringCalenderList;
	private ListView mWateringCalendarListView;
	
	private int addNewWaterPlan = 6; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.water_planer_listview);
		
		mWateringCalendarListView = getListView();
		mWateringCalendar = new WateringCalendarClass();
		
		mWateringCalenderList = new ArrayList<WateringCalendarClass>();
		
		setData(mWateringCalenderList);
		
		wca = new WateringCalendarAdapter(this, R.layout.water_calendar_list_row, mWateringCalenderList);
		mWateringCalendarListView.setAdapter(wca);
	}
	
	private void setData(ArrayList<WateringCalendarClass> list){
		WateringCalendarClass w = new WateringCalendarClass();
		w.setDate("Date:	26-07-2012");
		w.setWaterCalendarStartTime("Start time:	04.30");
		w.setWaterCalendarEndTime("End time:	05.00");
		list.add(w);
		
		w = new WateringCalendarClass();
		w.setDate("Date:	04-08-2012");
		w.setWaterCalendarStartTime("Start time:	23.30");
		w.setWaterCalendarEndTime("End time:	24.00");
		list.add(w);
		
		
		w = new WateringCalendarClass();
		w.setDate("Date:	26-08-2012");
		w.setWaterCalendarStartTime("Start time:	12.00");
		w.setWaterCalendarEndTime("End time:	12.30");
		list.add(w);
	}
	
	private class WateringCalendarAdapter extends ArrayAdapter<WateringCalendarClass>{
		private ArrayList<WateringCalendarClass> mItems;
		private Context mContext;
			
		public WateringCalendarAdapter(Context context, int textViewResourceId, 
				ArrayList<WateringCalendarClass> items){
			super(context, textViewResourceId, items);
			mItems = items;
			mContext = context;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent){
			View v = convertView;
			if(v == null){
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.water_calendar_list_row, null);
			}
			WateringCalendarClass WCC = mItems.get(position);
			if(WCC != null){
				TextView tt = (TextView) v.findViewById(R.id.water_startDate);
				if(tt != null){
					
					tt.setText(WCC.getWaterCalendarStartTime());
					tt.setTextColor(Color.GREEN);
				}
				TextView tv = (TextView) v.findViewById(R.id.water_StopDate);
				if(tv != null){
					tv.setText(WCC.getWaterCalendarEndTime());
					tv.setTextColor(Color.RED);
				}
				TextView tn = (TextView) v.findViewById(R.id.water_Name);
				if(tn != null)
				{
					tn.setText(WCC.getDate());
					
				}
			}
			return v;
		}
	}
	 public boolean onCreateOptionsMenu(Menu menu) 
		{
		    MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.menu.water_menu_view, menu);
		    return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) 
		{
		    // Handle item selection
		    switch (item.getItemId()) {
		    		
		        case R.id.Create:
		        	Intent intent = new Intent();
		    		intent.setClass(this, NewWaterPlanActivity.class);
		    		startActivityForResult(intent, addNewWaterPlan);
		            return true;
		        case R.id.Back:
		        	Intent intentToWateringAc = new Intent();
		        	intentToWateringAc.setClass(this,WateringActivity.class);
		        	startActivity(intentToWateringAc);
		        	
		            return true;
		      
		        default:
		            return super.onOptionsItemSelected(item);
		    }
		}
		
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			
			if(resultCode == RESULT_OK && requestCode == addNewWaterPlan) {
				
				WateringCalendarClass m = (WateringCalendarClass) data.getSerializableExtra("newWateringPlan");
				mWateringCalenderList.add(m);
				wca = new WateringCalendarAdapter(this, R.layout.water_calendar_list_row, mWateringCalenderList);
				mWateringCalendarListView.setAdapter(wca);
				
			}
			super.onActivityResult(requestCode, resultCode, data);
			
			
			
			
		}
}


